<?php
namespace Org\Oa;
/**
 * 购物车操作类
 * @author Administrator
 *
 */
class cartlist{
	/**
	 * 添加购物车
	 * @param unknown $goods_id 商品id
	 * @param unknown $productid 产品id
	 * @$num 产品的数量
	 */
	public static function add_cart($uid,$goods_id,$productid,$num){
		if($uid){
			$db = M('user_cart');
			$re = $db->where("goods_id='{$goods_id}' and product_id='{$productid}' and uid='{$uid}'")->find();
			if(!empty($re)){
				$db->where("id='{$re['id']}'")->setInc('num',$num);
			}else{
				$data['goods_id'] = $goods_id;
				$data['product_id'] = $productid;
				$data['num'] = $num;
				$data['uid'] = $uid;
				$db->add($data);
			}
		}
		$cart = session('shopping_cart');
		$cartlist = json_decode($cart,true);
		if(!empty($cartlist)){
			$flag = 1;
			foreach ($cartlist as $i=>$va){
				if($va['goods_id'] == $goods_id && $va['product_id'] == $productid){
					$cartlist[$i]['num'] = $va['num'] + $num;
					$flag *= 0;
				}else{
					$flag *= 1;
				}
			}
			if($flag){
				$count = count($cartlist);
				$cartlist[$count]['goods_id'] = $goods_id;
				$cartlist[$count]['product_id'] = $productid;
				$cartlist[$count]['num'] = $num;
			}
		}else{
			$cartlist[0]['goods_id'] = $goods_id;
			$cartlist[0]['product_id'] = $productid;
			$cartlist[0]['num'] = $num;
		}
		$carjson = json_encode($cartlist);
		session('shopping_cart',$carjson);
		
	}
	/**
	 * 读取购物车
	 */
	public static function read_cart($uid){
		$cart = session('shopping_cart');
		$cart = json_decode($cart,true);
		if($uid){
			$list = M('user_cart')->where("uid='{$uid}'")->field("goods_id,product_id,num")->order("id desc")->select();
			foreach ($list as $l=>$vl){
				foreach ($cart as $k=>$vk){
					if($vk['goods_id'] == $vl['goods_id'] && $vk['product_id'] == $vl['product_id']){
						unset($list[$l]);
					}
				}
			}
		}else{
			$list = array();
		}
		if(empty($cart)){
			$cartlist = $list;
		}else{
			$cartlist = array_merge($cart,$list);
		}
		if(!empty($cartlist)){
			$url = "http://".$_SERVER['HTTP_HOST'];
			foreach ($cartlist as $i=>$va){
				$newres[$i] = M('products as p')->join("left join __GOODS__ as g on g.id = p.goods_id")->where("p.goods_id='{$va['goods_id']}' and p.id='{$va['product_id']}' and p.store_nums>'{$va['num']}' and g.status='0' and g.is_del='0'")->field("g.name,p.sell_price,p.spec_array,g.img,p.photo_list,p.store_nums,p.id,p.goods_id,p.products_no")->find();
				if(!empty($newres[$i])){
					//获取优惠后的价格
					$true_price[$i] = \Org\Oa\promotion::product_for_promotion($uid, $newres[$i]['id'], 2, 0);
					
					if($true_price[$i]){
						$newres[$i]['sell_price'] = $true_price[$i];
					}
					$newres[$i]['num'] = $va['num'];
					if(!empty($newres[$i]['photo_list'])){
						$photo_list[$i] = explode(",",$newres[$i]['photo_list']);
						$newres[$i]['img'] = $url.$photo_list[$i][0];
					}else{
						$newres[$i]['img'] = $url.$newres[$i]['img'];
					}
				}else{
					//删除库存不够的
					unset($cartlist[$i]);
					unset($newres[$i]);
				}
			}
			$newres = array_values($newres);
		}else{
			$newres = array();
		}
		return $newres;
		
	}
	/**
	 * 获取购物车的数量（多少件商品）
	 */
	public static function count_cart($uid){
		$cart = self::read_cart($uid);
		return count($cart);
	}
	/**
	 * 删除购物车
	 */
	public static function del_cart($uid,$goods_id,$productid){
		$cart = session('shopping_cart');
		$cartlist = json_decode($cart,true);
		if(!empty($cartlist)){
			foreach ($cartlist as $i=>$va){
				if($va['goods_id'] == $goods_id && $va['product_id'] == $productid){
					unset($cartlist[$i]);
					break;
				}
			}
			$cartlist = array_values($cartlist);
			if(!empty($cartlist)){
				session('shopping_cart',json_encode($cartlist));
			}else{
				session('shopping_cart',null);
			}
		}
		if($uid){
			M('user_cart')->where("uid='{$uid}' and goods_id='{$goods_id}' and product_id ='{$productid}'")->delete();
		}
	}
}